﻿<%@ WebHandler Language="C#" Class="personalinitpwd" %>

using System;
using System.Web;

public class personalinitpwd : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        
        if (context.Request["op"] != null)
        {
            switch (context.Request["op"].ToLower())
            {
                case "createnewpassword":
                    CreateNewPassword(context);
                    break;
                case "changepwd":
                    ChangePwd(context);
                    break;
                case "handlelogin":
                    HandleLogin(context);
                    break;
            }
        }
        
    }

    /// <summary>
    /// 生成新密码
    /// </summary>
    /// <param name="context"></param>
    public void CreateNewPassword(HttpContext context)
    {
        string newPwd = GenerateRandom(6);
        
        context.Response.Write("{success:1,msg:'',data:{newpwd:'" + newPwd.ToLower() + "'}}");

    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    public void HandleLogin(HttpContext context)
    {
        try
        {

            if (context.Request["userid"] != null)
            {
                //声称requestkey 并放入缓存，过期时间设置为60分钟
                string strRequestKey = Guid.NewGuid().ToString().Replace("-", "");
                string strCachedKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_REQUEST, strRequestKey);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);

                cs.Set(strCachedKey, context.Request["userid"], DateTime.Now.AddMinutes(60));

                //用户登录
                //用户Id保存到Cookie中
                HttpCookie ckRequestKey = new HttpCookie("requestkey", strRequestKey);
                context.Response.AppendCookie(ckRequestKey);
                context.Response.Write("{success:1,msg:'',data:''}");
            }
            else
            {
                context.Response.Write("{success:0,msg:'登录信息设置失败，请重新登录！',data:''}");
            }
        }
        catch
        {
            context.Response.Write("{success:0,msg:'登录信息设置失败，请重新登录！',data:''}");
        }
    }

    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="context"></param>
    public void ChangePwd(HttpContext context)
    {
        try
        {
            Chenxin.Lib.Passport.Entity.Hr_UserInfo model = Chenxin.Lib.Passport.DAL.Hr_UserInfo.GetEntityById(new Guid(context.Request["userid"]));
            string oldPwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(context.Request["oldpwd"], "MD5");
            if (oldPwd == model.Password)
            {
                model.Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(context.Request["newpwd"], "MD5");
                model.PassUpdateTime = DateTime.Now;
                model.NeedChgPass = false;


                Chenxin.Lib.Passport.DAL.Hr_UserInfo.ChangePassword(model);
                context.Response.Write("{success:1,msg:'密码修改成功！',data:''}");
            }
            else
            {
                context.Response.Write("{success:0,msg:'原密码错误！',data:''}");
            }
        }
        catch
        {
            context.Response.Write("{success:0,msg:'密码修改失败！',data:''}");
        }
    }

    /// <summary>
    /// 生成随机数
    /// </summary>
    /// <param name="Length"></param>
    /// <returns></returns>
    public static string GenerateRandom(int Length)
    {
        char[] constant ={
　　　　　　　　　　　　　　'1','2','3','4','5','6','7','8','9',
　　　　　　　　　　　　　　'A','B','C','D','E','F','G','H','I','J',
　　　　　　　　　　　　　　'K','L','M','N','O','P','Q','R','S','T',
　　　　　　　　　　　　　　'U','V','W','X','Y','Z'
　　　　　　　　　　　　};
        System.Text.StringBuilder newRandom = new System.Text.StringBuilder(35);
        Random rd = new Random();
        for (int i = 0; i < Length; i++)
        {
            newRandom.Append(constant[rd.Next(35)]);
        }
        return newRandom.ToString();
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}